﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using

using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using System.Data.OracleClient;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

#endregion

namespace NetBay.Core.DataAccess
{
    /// <summary>
    /// 
    /// </summary>
    public class DbHelper
    {

        private const string ORACLE_CONNECTIONSTRING_NAME = "RIO.BDD.Oracle";
        private const string SQLSERVER_CONNECTIONSTRING_NAME = "RIO.BDD.SQLServer";

        #region Private members
        /// <summary>
        /// 
        /// </summary>
        private static string s_sPath = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private static string s_sConfigFullFilePath = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private static string s_sDataBaseConnection = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        private static IConfigurationSource s_oIConfigurationSource = null;
        /// <summary>
        /// 
        /// </summary>
        private static DatabaseProviderFactory s_oDatabaseProviderFactory = null;

        private static Database s_CurrentDatabase = null; 
        #endregion

        #region Properties
        public static Database DataBase
        {
            get {

                if (s_CurrentDatabase == null)
                {
                    using (s_oDatabaseProviderFactory = new DatabaseProviderFactory(s_oIConfigurationSource))
                    {
                        s_CurrentDatabase = s_oDatabaseProviderFactory.Create(s_sDataBaseConnection);
                    }
                }
                return s_CurrentDatabase;
                
            }
        }
        #endregion

        #region Method

        public static void Init(string p_sDirectory, string p_sFileName, string p_sDataBaseConnection)
        {
            s_sConfigFullFilePath = Path.Combine(p_sDirectory, p_sFileName);
            s_oIConfigurationSource = new  FileConfigurationSource(s_sConfigFullFilePath);
            s_sDataBaseConnection = p_sDataBaseConnection;
        }

        /// <summary>
        /// Inits the specified rio BDD string type.
        /// </summary>
        /// <param name="rioBddStringType">Type of the rio BDD string.</param>
        /// <param name="connectionString">The connection string.</param>
        /// <returns></returns>
        public static Database Init(string rioBddStringType, string connectionString)
        {
            switch (rioBddStringType)
            {
                case ORACLE_CONNECTIONSTRING_NAME:
                    return new OracleDatabase(connectionString);
                case SQLSERVER_CONNECTIONSTRING_NAME:
                    return new SqlDatabase(connectionString);
            }
            return null;
        }

        /// <summary>
        /// Add oracle result parmeter to the command
        /// </summary>
        /// <param name="dbCommand"></param>
        public static void AddOracleResultParameter(ref DbCommand dbCommand)
        {
            var l_oOracleOutputParameter = new OracleParameter("O_Results", OracleType.Cursor);
            l_oOracleOutputParameter.Direction = ParameterDirection.Output;
            dbCommand.Parameters.Add(l_oOracleOutputParameter);
        }

        #endregion
    }
}
